package com.pay.game.central.web.mapper;

import com.github.pagehelper.Page;
import com.pay.game.central.web.bean.Settlement;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface SettlementMapper {

    @Insert("<script>" +
            "INSERT INTO settlement(store_id, start_time, start_date, store_name, user_id, user_name) VALUES" +
            "  <foreach collection=\"list\" item=\"item\" separator=\",\"> "+
            "   (#{item.store_id},#{item.start_time,jdbcType=INTEGER},#{item.start_date,jdbcType=DATE}," +
            "     #{item.store_name},#{item.user_id},#{item.user_name})" +
            "  </foreach>"+
            "</script>"
    )
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void addSettlements(List<Settlement> list);

    @Update("update settlement set " +
            "   value=#{value, jdbcType=INTEGER}," +
            "   score=#{score, jdbcType=INTEGER}," +
            "   run_score=#{run_score, jdbcType=INTEGER}," +
            "   rate=#{rate, jdbcType=FLOAT}, " +
            "   stop_time=#{stop_time, jdbcType=INTEGER}," +
            "   stop_date=#{stop_date, jdbcType=DATE}," +
            "   status=1" +
            " where id=#{id, jdbcType=BIGINT}")
    void updateSettlementToStop(Settlement settlement);

    /**
     * 获取需要结算的信息
     * @param ids 需要的数据ID
     * @return
     */
    @Select("<script>" +
            "   select id,store_id from settlement where id in" +
            "   <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\" >" +
            "       #{item,jdbcType=BIGINT} " +
            "   </foreach>" +
            "</script>")
    List<Settlement> queryByIds(@Param("ids") List<Long> ids);

    @Select("<script>" +
            "  select id,store_id,store_name,user_name,user_id,rate,score,run_score,value,stop_date from settlement where status = 1 " +
            "  <if test='showOneDay'>" +
            "   and stop_date = #{endDate}" +
            "  </if>" +
            "  <if test='!showDescendant'>" +
            "   and user_id = #{rootUserId}" +
            "  </if>" +
            "  <if test=\"storeId!='' and storeId!=null\">" +
            "   and store_id like concat(#{storeId},'%')" +
            "  </if>" +
            "  <if test=\"storeName!='' and storeName!=null\">" +
            "   and store_name like concat(#{storeName},'%')" +
            "  </if>" +
            "  <if test='showDescendant'>" +
            "   and user_id like concat(#{rootUserId},'%')" +
            "  </if>" +
            "  <if test='!showOneDay'>" +
            "   and stop_date &lt;= #{endDate}" +
            "   and stop_date &gt;= #{startDate}" +
            "  </if>" +
            "</script>")
    Page<Settlement> list(@Param("rootUserId") String rootUserId,
                          @Param("showDescendant") boolean showDescendant,
                          @Param("storeId") String storeId,
                          @Param("storeName") String storeName,
                          @Param("showOneDay") boolean showOneDay,
                          @Param("startDate") String startDate,
                          @Param("endDate") String endDate);

    @Select("select run_score, `value` from settlement where user_id=#{userId} and stop_date <= #{endDate} and stop_date >= #{startDate}")
    List<Settlement> listPeriod(@Param("userId") String userId, @Param("startDate") String startDate, @Param("endDate") String endDate);
}
